package cj.com.chabaike.ui.frag;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;

import java.util.Calendar;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cj.com.chabaike.R;
import cj.com.chabaike.bll.PandaPagerManager;
import cj.com.chabaike.bll.RWServer;
import cj.com.chabaike.dao.FetchJSONDAO;
import cj.com.chabaike.entity.NewsList;
import cj.com.chabaike.entity.special.Special_detail_data;
import cj.com.chabaike.entity.special.Special_detail_data_list;
import cj.com.chabaike.entity.special.Special_detail_data_list_conlist;
import cj.com.chabaike.ui.act.MainPager;
import cj.com.chabaike.ui.adapter.BaseRecyclerAdapter;
import cj.com.chabaike.ui.utils.HttpBitMapHelp;
import cj.com.chabaike.ui.utils.Navigator;
import cj.com.chabaike.ui.utils.TransitionHelper;

/**
 * Created by CJLAB on 2015/8/18.
 */
public class Special_Detail extends TransitionHelper.BaseFragment  {

    /*************************全局变量声明区*******************************/

    @InjectView(R.id.special_detail_topimg)
    ImageView special_detail_topimg;//顶部图片
    @InjectView(R.id.special_detail_tvparentlayout)
    LinearLayout special_detail_tvparentlayout;//简介说明文字的父布局
    @InjectView(R.id.special_detail_tv)
    TextView special_detail_tv;//简介说明文字
    @InjectView(R.id.special_detail_recyclerView)
    RecyclerView mRecyclerView;
    @InjectView(R.id.special_detail_rootlayout)
    LinearLayout rootLayout;


    private BitmapUtils bitmapUtils;
    PandaPagerManager ppm;
    RWServer rws;
    String Special_detail_data_url;
    Special_detail_data_list mSpecial_detail_data_list;//数据源
    List<Special_detail_data_list_conlist> mSpecial_detail_data_list_conlist;//RecylcerView数据源
    SpecialDetailDataRecyclerAdapter mSpecialDetailDataRecyclerAdapter;

    /*************************全局变量声明区结束*******************************/
    /*************************生命周期方法区*******************************/
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ppm = PandaPagerManager.getPandaPagerManagerInstance();

    }


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        Special_detail_data_url = activity.getIntent().getStringExtra("Special_detail_data_url");
        rws = RWServer.getInstance(activity.getApplicationContext());
        bitmapUtils =  HttpBitMapHelp.getBitmapUtils(getActivity().getApplicationContext());
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.special_detail_pager,null,false);
        ButterKnife.inject(this, rootView);
        initRecyclerView();
        initDataSource();


        return rootView;
    }

    /*************************生命周期方法区结束*******************************/
    /*************************init区*******************************/


    private void initDataSource() {
        rws.getSepcialDetailData(ppm.getSpecialDetailUrl(Special_detail_data_url), new FetchJSONDAO.SpecialDataDetailResultBack() {
            @Override
            public void specialDetailResultWhat(boolean isSuccusful, Special_detail_data special_detail_dataData) {
                if (isSuccusful) {
                    if (special_detail_dataData != null) {
                        if (special_detail_dataData.getList() != null) {
                            if (special_detail_dataData.getList().get(0).getConlist() != null) {
                                mSpecial_detail_data_list = special_detail_dataData.getList().get(0);
                                mSpecial_detail_data_list_conlist = mSpecial_detail_data_list.getConlist();

                                initDatailViews();
                            }
                        }
                    }


                } else {


                }
            }


        });
    }

    private void initDatailViews() {
        bitmapUtils.display(special_detail_topimg, mSpecial_detail_data_list.getSectionimage());
        if(mSpecial_detail_data_list.getSectionintroduce().length()<1){
            special_detail_tvparentlayout.setVisibility(View.GONE);
        }
        special_detail_tv.setText(mSpecial_detail_data_list.getSectionintroduce());
        mSpecialDetailDataRecyclerAdapter.updateList(mSpecial_detail_data_list.getConlist());
    }

    private void initRecyclerView(){
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mSpecialDetailDataRecyclerAdapter= new SpecialDetailDataRecyclerAdapter();
        mRecyclerView.setAdapter(mSpecialDetailDataRecyclerAdapter);
        mSpecialDetailDataRecyclerAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener<Special_detail_data_list_conlist>() {
            @Override
            public void onItemClick(View view, Special_detail_data_list_conlist item, boolean isLongClick) {
                Navigator.launchDetailSeach(MainPager.of(getActivity()), view, item, MainPager.of(getActivity()).fragmentBackround);
            }
        });



    }


    /*************************init区结束*******************************/
    /*************************方法调用区*******************************/
    /*************************方法调用区结束*******************************/


    /**
     * ******************内部类区*******************************
     */


    /*recylcerView的适配器 使用带有cardView的适配器 第二层继承*/

    class SpecialDetailDataRecyclerAdapter extends BaseRecyclerAdapter<Special_detail_data_list_conlist> {

        @Override
        public ThingHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recylcer_item_newstype, parent, false);
            return new ThingHolder(view);
        }


        public class ThingHolder extends BaseRecyclerAdapter<Special_detail_data_list_conlist>.ViewHolder {
            @InjectView(R.id.item_news_title)
            TextView titleTextView;

            @InjectView(R.id.item_news_ItemViewKeyWord)
            TextView keyWorldTextView;

            @InjectView(R.id.item_news_commentNum)
            TextView commentNumTextView;

            @InjectView(R.id.item_news_ItemViewImg0)
            ImageView ItemViewImg;


            public ThingHolder(View itemView) {
                super(itemView);
            }

            public void populate(Special_detail_data_list_conlist item) {
                titleTextView.setText(item.getTitle());
                commentNumTextView.setText(item.getCommentNum());
                keyWorldTextView.setText(getTimedata(item.getCreatedDate()));

                if (item.getImgurl().length()>0) {
                    bitmapUtils.display(ItemViewImg, item.getImgurl());

                }

            }

            private String getTimedata(String createDate) {
                Calendar c = Calendar.getInstance();
                int newsYear = 0, newsMonth = 0, newsDay = 0, newsHour = 0, newsMin = 0;
                int year = c.get(Calendar.YEAR);
                int month = c.get(Calendar.MONTH) + 1;
                int day = c.get(Calendar.DAY_OF_MONTH)-10;
                int hour = c.get(Calendar.HOUR_OF_DAY);
                int min = c.get(Calendar.MINUTE);

                try {
                    newsYear = Integer.parseInt(createDate.substring(0, 4));
                    // Log.e("newsYear", "" + newsYear);
                    newsMonth = Integer.parseInt(createDate.substring(6, 7));
                    // Log.e("newsMonth", "" + newsMonth);
                    newsDay = Integer.parseInt(createDate.substring(9, 10));
                    // Log.e("newsDay", "" + newsDay);
                    newsHour = Integer.parseInt(createDate.substring(12, 13));
                    //Log.e("newsHour", "" + newsHour);
                    newsMin = Integer.parseInt(createDate.substring(15, 16));
                    // Log.e("newsMin", "" + newsMin);
                } catch (Exception e) {
                    // Log.e("getTimedata", "截取json时间出错");
                    //  e.printStackTrace();
                }
                if (year - newsYear > 0) {
                    return "很久很久之前";

                } else if (month - newsMonth > 0) {
                    return (month - newsMonth) + "个月前";
                } else if (day - newsDay > 0) {
                    return (day - newsDay) + "天前";
                } else if (hour - newsHour > 0) {
                    return (hour - newsHour) + "小时前";
                } else if (min - newsMin > 0) {
                    return (min - newsMin) + "分钟前";
                }

                return "不知道多久之前";
            }

        }
    }


    /*********************内部类区结束********************************/

    @Override
    public void onBeforeViewShows(View contentView) {
        ViewCompat.setTransitionName(rootLayout, "detail_element");
        // ViewCompat.setTransitionName(getActivity().findViewById(R.id.fab), "fab");
        //  BaseActivity.of(getActivity()).fab.setTranslationY(400);

        //  TransitionHelper.excludeEnterTarget(getActivity(), R.id.toolbar_container, true);
        TransitionHelper.excludeEnterTarget(getActivity(), R.id.mainlayout, true);
    }

    @Override
    public void onBeforeEnter(View contentView) {
        MainPager.of(getActivity()).fragmentBackround.animate().scaleX(.92f).scaleY(.92f).alpha(.6f).setDuration(Navigator.ANIM_DURATION).setInterpolator(new AccelerateInterpolator()).start();
        //  BaseActivity.of(getActivity()).setHomeIcon(MaterialMenuDrawable.IconState.BURGER);
        //  BaseActivity.of(getActivity()).animateHomeIcon(MaterialMenuDrawable.IconState.ARROW);
    }

    @Override
    public boolean onBeforeBack() {
        //   MainPager.of(getActivity()).animateHomeIcon(MaterialMenuDrawable.IconState.BURGER);
        MainPager.of(getActivity()).fragmentBackround.animate().scaleX(1).scaleY(1).alpha(1).translationY(0).setDuration(Navigator.ANIM_DURATION / 4).setInterpolator(new DecelerateInterpolator()).start();
        TransitionHelper.fadeThenFinish(special_detail_tv, getActivity());
        return false;
    }
}
